package com.freight.order.utils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static void main(String[] args) {
//        DateFormat df = new SimpleDateFormat("yyyyMMdd/HHmmss");
//        Date date = new Date();
//        System.out.println(df.format(date));

//        String dateTime = "20140325/104954";
//        String dateTime_e = "20140326/094954";
//        System.out.println(timeDay(dateTime, dateTime_e));
//        Date date = new Date();
//        System.out.println(dateFBD(date));
//        getTrackInfo(dateTime, dateTime_e);
//        System.out.println(addDay(dateTime));



    }

    public static String getTrackInfo(String startTime, String endTime) {
        String str = "";
        long i = timeDay(startTime, endTime) + 1;
        if (i == 1) {

        } else {
            for (long k = 0; k <= i; k++) {

            }
            if (timeC(startTime, endTime) > 1440) {
                String startTime_t = addDay(startTime);
                str = getTrackInfo(startTime_t, endTime);
                System.out.println("if: " + str);
            } else {
                str = str + 1;
                System.out.println(str);
            }
        }
        return str;

    }

    /**
     * 时间加23小时59分
     *
     * @param dateTime
     * @return yyyyMMdd/HHmmss
     */
    public static String addDay(String dateTime) {
        String addTime = "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar rightNow = Calendar.getInstance();
        try {
            Date date = df.parse(dateTime);
            rightNow.setTime(date);
            rightNow.add(Calendar.HOUR, 23);
            rightNow.add(Calendar.MINUTE, 59);
            addTime = df.format(rightNow.getTime());
        } catch (ParseException e) {
//            e.printStackTrace();
            System.out.println("时间转换异常+ addDay");
        }
        return addTime;
    }


    public static long timeC(String startTime, String endTime) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd/HHmmss");
        long mines = 0;
        try {
            Date d1 = df.parse(endTime);
            Date d2 = df.parse(startTime);
            long diff = d1.getTime() - d2.getTime();
            mines = diff / (1000 * 60);
        } catch (Exception e) {
            System.out.println("时间转换异常 timeC——date");
        }
        return mines;
    }

    public static long timeDay(String startTime, String endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long mines = 0;
        try {
            Date d1 = df.parse(endTime);
            Date d2 = df.parse(startTime);
            long diff = d1.getTime() - d2.getTime();
            mines = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            System.out.println("时间转换异常 timeDay");
        }
        return mines;
    }

    public static long timeC(Date startTime, Date endTime) {
        long mines = 0;
        try {
            long diff = endTime.getTime() - startTime.getTime();
            mines = diff / (1000 * 60);
        } catch (Exception e) {
            System.out.println("时间转换异常 timeC_date");
        }
        return mines;
    }

    public static long timeM(String maxDate) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd/HHmmss");
        Date nowDate = new Date();
        long mines = 0;
        try {
            long diff = nowDate.getTime() - df.parse(maxDate).getTime();
            mines = diff / (1000 * 60);
        } catch (Exception e) {
            System.out.println("时间转换异常 timeM");
        }
        return mines;
    }

    public static String dateF(Date dateTime){
        DateFormat df = new SimpleDateFormat("yyyyMMdd/HHmmss");
        String stime = null;
        try {
             stime = df.format(dateTime);
        } catch (Exception e) {
            System.out.println("时间转换异常 dateF");
        }
        return stime;
    }

    public static String dateFBD(Date dateTime){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stime = null;
        try {
            stime = df.format(dateTime);
        } catch (Exception e) {
            System.out.println("时间转换异常 dateFBD");
        }
        return stime;
    }

    public static long timeM(Date getTime) {
//        DateFormat df = new SimpleDateFormat("yyyyMMdd/HHmmss");
        Date nowDate = new Date();
        long mines = 0;
        try {
            long diff = nowDate.getTime() - getTime.getTime();
            mines = diff / (1000 * 60);
        } catch (Exception e) {
            System.out.println("时间转换异常 timeM");
        }
        return mines;
    }

}
